﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using System.Data;
using Air_condition.DO;
using ColdStorage.DOL;
using System.Reflection;
using Report.CondenserData;
using ThermalEquipmentDesign.Model.Condenser.CoolAirR22;
using ThermalEquipmentDesign.Model.Condenser.EvaporaterR717;
using ThermalEquipmentDesign.Model.Condenser.SprinklerR717;
using ThermalEquipmentDesign.Model.Condenser.VerticalPipeR717;
using ThermalEquipmentDesign.Model.Condenser.HorizontalPipeR12;
using ThermalEquipmentDesign.Model.CasingPipeSprinklers;
using ThermalEquipmentDesign.Model.CasingPipeWing;
using ThermalEquipmentDesign.Model.CoolerWet;
using ThermalEquipmentDesign.Model.PlateAmoniac;
using ThermalEquipmentDesign.Model.SmoothPipeR717R22;
using Report.EvaporatorData;
using Report.CoolingTowerData;
using ThermalEquipmentDesign.Model.CoolingTower;

namespace Report
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainReport : Window
    {
        ReportDocument Report = new ReportDocument();

        TreeViewItem tvCold = new TreeViewItem();

        DSCold dscold = new DSCold();
        DSAir dsair = new DSAir();

        public MainReport()
        {
            InitializeComponent();
        }

        #region Report for AirCondition and ColdStorage
        public void LoadCrystalAir()
        {
            dsair.Tables[0].Rows.Add();
        }

        public void LoadCrystalCold(Cold_Storage cs)
        {

            tvCold.Header = "Cold Storage";
            tvCold.Tag = cs;
            tvCold.IsExpanded = true;
            treeView1.Items.Add(tvCold);
            dscold.Tables[0].Rows.Add();
        }

        private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            int db = 0;
            if (treeView1.SelectedItem != null && ((TreeViewItem)treeView1.SelectedItem).Tag is Air_condition.DO.Room)
            {
                Air_condition.DO.Result rs = ((Air_condition.DO.Room)((TreeViewItem)treeView1.SelectedItem).Tag).res;
                foreach (PropertyInfo i in rs.GetType().GetProperties())
                {
                    dsair.Tables[0].Rows[0][db] = rs.GetType().GetProperty(i.Name).GetValue(rs, null);
                    db++;
                }
                Alternative alt = ((Alternative)((TreeViewItem)((TreeViewItem)treeView1.SelectedItem).Parent).Tag);
                dsair.Tables[0].Rows[0][db] = alt.sProject;
                db++;
                dsair.Tables[0].Rows[0][db] = alt.sLocation;
                db++;
                dsair.Tables[0].Rows[0][db] = alt.sBuildingowner;
                db++;
                dsair.Tables[0].Rows[0][db] = alt.sDescription;
                db++;
                dsair.Tables[0].Rows[0][db] = alt.dbColdProductivity;
                ViewReport(ReportPath.AirReportPath, dsair.Tables[0]);
            }
            else
            {
                if (treeView1.SelectedItem != null && ((TreeViewItem)treeView1.SelectedItem).Tag is ColdStorage.DOL.Room)
                {
                    ColdStorage.DOL.Result rs = ((ColdStorage.DOL.Room)((TreeViewItem)treeView1.SelectedItem).Tag).res;
                    foreach (PropertyInfo i in rs.GetType().GetProperties())
                    {
                        dscold.Tables[0].Rows[0][db] = rs.GetType().GetProperty(i.Name).GetValue(rs, null);
                        db++;
                    }
                    Cold_Storage cold = ((Cold_Storage)((TreeViewItem)((TreeViewItem)treeView1.SelectedItem).Parent).Tag);
                    dscold.Tables[0].Rows[0][db] = cold.sProjectName;
                    db++;
                    dscold.Tables[0].Rows[0][db] = cold.sLocation;
                    db++;
                    dscold.Tables[0].Rows[0][db] = cold.sProjectOwner;
                    db++;
                    dscold.Tables[0].Rows[0][db] = cold.sDescription;
                    db++;
                    dscold.Tables[0].Rows[0][db] = cold.dbResult;
                    ViewReport(ReportPath.ColdReportPath, dscold.Tables[0]);
                }
            }
        }

        public void LoadTreeView(object o)
        {
            if (o is Alternative)
            {
                Alternative alt = (Alternative)o;
                TreeViewItem tv = new TreeViewItem();
                tv.IsExpanded = true;
                tv.Header = alt.sName;
                tv.Tag = alt;
                foreach (Air_condition.DO.Room r in alt.lRoom)
                {
                    TreeViewItem tr = new TreeViewItem();
                    tr.Header = r.sName;
                    tr.Tag = r;
                    tv.Items.Add(tr);
                }
                treeView1.Items.Add(tv);
            }
            else
            {
                TreeViewItem tv = new TreeViewItem();
                tv.Header = ((ColdStorage.DOL.Room)o).sRoomName;
                tv.Tag = (ColdStorage.DOL.Room)o;
                tvCold.Items.Add(tv);
            }
        }
        #endregion

        #region Report for Condenser
        public void LoadReportCoolAirR22(CoolAirR22Result oCoolAirR22Result)
        {
            DataCoolAirR22 dsCoolAirR22 = new DataCoolAirR22();
            dsCoolAirR22.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oCoolAirR22Result.GetType().GetProperties())
            {
                dsCoolAirR22.Tables[0].Rows[0][db] = oCoolAirR22Result.GetType().GetProperty(i.Name).GetValue(oCoolAirR22Result, null);
                db++;
            }            
            ViewReport(ReportPath.ReportCoolAirR22,dsCoolAirR22.Tables[0]);
        }

        public void LoadReportEvaporaterR717(EvaporaterR717Result oEvaporaterR717Result)
        {
            DataEvaporatorR717 dsEvaporatorR717 = new DataEvaporatorR717();
            dsEvaporatorR717.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oEvaporaterR717Result.GetType().GetProperties())
            {
                dsEvaporatorR717.Tables[0].Rows[0][db] = oEvaporaterR717Result.GetType().GetProperty(i.Name).GetValue(oEvaporaterR717Result, null);
                db++;
            }
            ViewReport(ReportPath.ReportEvaporatorR717, dsEvaporatorR717.Tables[0]);
        }

        public void LoadReportHorizontalPipeR12(HorizontalPipeR12Result oHorizontalPipeR12Result)
        {
            DataHorizontalPipeR12 dsHorizontalPipeR12 = new DataHorizontalPipeR12();
            
            dsHorizontalPipeR12.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oHorizontalPipeR12Result.GetType().GetProperties())
            {
                dsHorizontalPipeR12.Tables[0].Rows[0][db] = oHorizontalPipeR12Result.GetType().GetProperty(i.Name).GetValue(oHorizontalPipeR12Result, null);
                db++;
            }
            ViewReport(ReportPath.ReportHorizontalPipeR12, dsHorizontalPipeR12.Tables[0]);
        }

        public void LoadReportSprinklerR717(SprinklerR717Result oSprinklerR717Result)
        {
            DataSprinklerR7171 dsSprinklerR717 = new DataSprinklerR7171();
            dsSprinklerR717.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oSprinklerR717Result.GetType().GetProperties())
            {
                dsSprinklerR717.Tables[0].Rows[0][db] = oSprinklerR717Result.GetType().GetProperty(i.Name).GetValue(oSprinklerR717Result, null);
                db++;
            }
            ViewReport(ReportPath.ReportSprinklerR717, dsSprinklerR717.Tables[0]);
        }

        public void LoadReportVerticalPipeR717(VerticalPipeR717Result oVerticalPipeR717Result)
        {
            DataVerticalPipeR717 dsVerticalPipeR717 = new DataVerticalPipeR717();
            dsVerticalPipeR717.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oVerticalPipeR717Result.GetType().GetProperties())
            {
                dsVerticalPipeR717.Tables[0].Rows[0][db] = oVerticalPipeR717Result.GetType().GetProperty(i.Name).GetValue(oVerticalPipeR717Result, null);
                db++;
            }
            ViewReport(ReportPath.ReportVerticalPipeR717, dsVerticalPipeR717.Tables[0]);
        }
        #endregion

        #region Report for Evaporater
        public void LoadReportCasingPipeSprinklers(CasingPipeSprinklersResult oCasingPipeSprinklersResult)
        {
            DataCasingPipeSprinklers dsCasingPipeSprinklers = new DataCasingPipeSprinklers();
            dsCasingPipeSprinklers.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oCasingPipeSprinklersResult.GetType().GetProperties())
            {
                dsCasingPipeSprinklers.Tables[0].Rows[0][db] = oCasingPipeSprinklersResult.GetType().GetProperty(i.Name).GetValue(oCasingPipeSprinklersResult, null);
                db++;
            }
            ViewReport(ReportPath.ReportCasingPipeSprinklers, dsCasingPipeSprinklers.Tables[0]);
        }

        public void LoadReportCasingPipeWing(CasingPipeWingResult oCasingPipeWingResult)
        {
            DataCasingPipeWing dsCasingPipeWing = new DataCasingPipeWing();
            dsCasingPipeWing.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oCasingPipeWingResult.GetType().GetProperties())
            {
                dsCasingPipeWing.Tables[0].Rows[0][db] = oCasingPipeWingResult.GetType().GetProperty(i.Name).GetValue(oCasingPipeWingResult, null);
                db++;
            }
            ViewReport(ReportPath.ReportCasingPipeWing, dsCasingPipeWing.Tables[0]);
        }

        public void LoadReportCoolerWet(CoolerWetResult oCoolerWetResult)
        {
            DataCoolerWet dsCoolerWet = new DataCoolerWet();
            dsCoolerWet.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oCoolerWetResult.GetType().GetProperties())
            {
                dsCoolerWet.Tables[0].Rows[0][db] = oCoolerWetResult.GetType().GetProperty(i.Name).GetValue(oCoolerWetResult, null);
                db++;
            }
            ViewReport(ReportPath.ReportCoolerWet, dsCoolerWet.Tables[0]);
        }

        public void LoadReportPlateAmoniac(PlateAmoniacResult oPlateAmoniacResult)
        {
            DataPlateAmoniac dsPlateAmoniac = new DataPlateAmoniac();            
            dsPlateAmoniac.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oPlateAmoniacResult.GetType().GetProperties())
            {
                dsPlateAmoniac.Tables[0].Rows[0][db] = oPlateAmoniacResult.GetType().GetProperty(i.Name).GetValue(oPlateAmoniacResult, null);
                db++;
            }
            ViewReport(ReportPath.ReportPlateAmoniac, dsPlateAmoniac.Tables[0]);
        }

        public void LoadReportSmoothPiperR717R22(SmoothPipeR717R22Result oSmoothPipeR717R22Result)
        {
            DataSmoothPipeR717R22 dsSmoothPipeR717R22 = new DataSmoothPipeR717R22();
            dsSmoothPipeR717R22.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oSmoothPipeR717R22Result.GetType().GetProperties())
            {
                dsSmoothPipeR717R22.Tables[0].Rows[0][db] = oSmoothPipeR717R22Result.GetType().GetProperty(i.Name).GetValue(oSmoothPipeR717R22Result, null);
                db++;
            }
            ViewReport(ReportPath.ReportSmoothPiperR717R22, dsSmoothPipeR717R22.Tables[0]);
        }
        #endregion

        #region Report for Cooling Tower
        public void LoadReportCoolingTower(CoolingtowerResult oCoolingtowerResult)
        {
            DataCoolingTower dsCoolingTower = new DataCoolingTower();
            dsCoolingTower.Tables[0].Rows.Add();
            int db = 0;
            foreach (PropertyInfo i in oCoolingtowerResult.GetType().GetProperties())
            {
                dsCoolingTower.Tables[0].Rows[0][db] = oCoolingtowerResult.GetType().GetProperty(i.Name).GetValue(oCoolingtowerResult, null);
                db++;
            }
            ViewReport(ReportPath.ReportCoolingTower, dsCoolingTower.Tables[0]);
        }
        #endregion

        private void ViewReport(string ReportName,DataTable dt)
        {
            Report.Load(ReportName);
            Report.SetDataSource(dt);
            crystalReportsViewer1.ViewerCore.ReportSource = Report;
        }
    }
}
